package com.example.cardgame_1

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class SecondActivity : AppCompatActivity() {
    // 声明控件变量
    private lateinit var tvStudentId: TextView
    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvEmail: TextView
    private lateinit var modifyBtn: Button
    private lateinit var game1Btn: Button
    private lateinit var game2Btn: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_second)

        // 初始化控件
        initViews()

        // 接收MainActivity传递的数据
        receiveData()

        // 修改按钮点击事件：返回MainActivity
        modifyBtn.setOnClickListener {
            finish() // 关闭当前页面，返回上一页
        }

        // 游戏1按钮点击事件：启动ThirdActivity
        game1Btn.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }

        // 游戏2按钮点击事件：启动FourthActivity
        game2Btn.setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
            startActivity(intent)
        }

        // 处理系统栏Insets
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    // 初始化所有控件
    private fun initViews() {
        tvStudentId = findViewById(R.id.tv_student_id)
        tvName = findViewById(R.id.tv_name)
        tvGender = findViewById(R.id.tv_gender)
        tvPhone = findViewById(R.id.tv_phone)
        tvEmail = findViewById(R.id.tv_email)
        modifyBtn = findViewById(R.id.btn_modify_second)
        game1Btn = findViewById(R.id.btn_start_game1)
        game2Btn = findViewById(R.id.btn_start_game2)
    }

    // 接收并显示数据
    private fun receiveData() {
        val studentId = intent.getStringExtra("STUDENT_ID") ?: "未填写"
        val name = intent.getStringExtra("NAME") ?: "未填写"
        val gender = intent.getStringExtra("GENDER") ?: "未填写"
        val phone = intent.getStringExtra("PHONE") ?: "未填写"
        val email = intent.getStringExtra("EMAIL") ?: "未填写"

        // 显示数据（拼接提示文本）
        tvStudentId.text = getString(R.string.tv_student_id) + studentId
        tvName.text = getString(R.string.tv_name) + name
        tvGender.text = getString(R.string.tv_gender) + gender
        tvPhone.text = getString(R.string.tv_phone) + phone
        tvEmail.text = getString(R.string.tv_email) + email
    }
}